package com.example.dell.fastshopping.home;

import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.dell.fastshopping.R;
import com.example.dell.fastshopping.mall.DuoBaoMallActivity;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoaderInterface;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainFragment extends Fragment {

    @BindView(R.id.banner)
    Banner banner;
    private MainViewModel mViewModel;
    private Context mContext;

    public static MainFragment newInstance() {
        return new MainFragment();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.main_fragment, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
        initView();
    }

    private void initView() {
        ArrayList<Integer> bannerList = mViewModel.getBannerList();
        //设置图片网址或地址的集合
        banner.setImages(bannerList);
        //设置轮播间隔时间
        banner.setDelayTime(3000);
        //设置是否为自动轮播，默认是“是”。
        banner.isAutoPlay(true);
        banner.setBannerStyle(BannerConfig.CENTER);
        //设置轮播的动画效果，内含多种特效，可点入方法内查找后内逐一体验
        banner.setBannerAnimation(Transformer.Default);
        banner.setImageLoader(new ImageLoaderInterface() {
            @Override
            public void displayImage(Context context, Object path, View imageView) {
                Glide.with(context).load(path).centerCrop().into((ImageView) imageView);
            }

            @Override
            public View createImageView(Context context) {
                return null;
            }
        });
        //设置指示器的位置，小点点，左中右。
        banner.setIndicatorGravity(BannerConfig.CENTER)
                //以上内容都可写成链式布局，这是轮播图的监听。比较重要。方法在下面。
                .setOnBannerListener(new OnBannerListener() {
                    @Override
                    public void OnBannerClick(int position) {

                    }
                })
                //必须最后调用的方法，启动轮播图。
                .start();
    }

//    @OnClick({R.id.home_jewely, R.id.home_coin, R.id.home_draw, R.id.home_old,  R.id.one, R.id.two, R.id.three, R.id.four})
//    public void onViewClicked(View view) {
//        Intent intent = new Intent(mContext, DuoBaoMallActivity.class);
//        startActivity(intent);
//        switch (view.getId()) {
//            case R.id.home_jewely:
//                break;
//            case R.id.home_coin:
//                break;
//            case R.id.home_draw:
//                break;
//            case R.id.home_old:
//                break;
//
//            case R.id.one:
//                break;
//            case R.id.two:
//                break;
//            case R.id.three:
//                break;
//            case R.id.four:
//                break;
//        }
//    }
}
